package io.renren.modules.sys.service.impl;

import io.renren.common.utils.Constant;
import io.renren.modules.sys.entity.OnshelvesStatisticsEntity;
import io.renren.modules.sys.entity.OnshelvesVOEntity;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.OnshelvesDao;
import io.renren.modules.sys.entity.OnshelvesEntity;
import io.renren.modules.sys.service.OnshelvesService;


@Service("onshelvesService")
public class OnshelvesServiceImpl extends ServiceImpl<OnshelvesDao, OnshelvesEntity> implements OnshelvesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String sheelvesid = (String)params.get("shelvesid");
        String number = (String)params.get("number");
        String statu = (String)params.get("statu");
        String startime = (String)params.get("startime");
        String endtime = (String)params.get("endtime");
        IPage<OnshelvesEntity> page = this.page(
                new Query<OnshelvesEntity>().getPage(params),
                new QueryWrapper<OnshelvesEntity>().like(StringUtils.isNotBlank(number),"upperNumber",number)
                        .eq(StringUtils.isNotBlank(sheelvesid),"shelvesid",sheelvesid)
                        .eq(StringUtils.isNotBlank(statu),"statu",statu)
                        .gt(StringUtils.isNotBlank(startime),"createtime",startime)
                        .lt(StringUtils.isNotBlank(endtime),"createtime",endtime)
                        .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );

        return new PageUtils(page);
    }

    @Override
    public OnshelvesVOEntity getInfoById(Integer id){
        OnshelvesVOEntity onshelvesVOEntity = baseMapper.getInfoById(id);

        return onshelvesVOEntity;
    }

    @Override
    public OnshelvesStatisticsEntity getStatistics(Map<String, Object> params){
        Integer shelvesid = 0;
        if(params.get("shelvesid")!=null && !params.get("shelvesid").equals("")){
            shelvesid = Integer.parseInt(params.get("shelvesid").toString());
        }
        String number = null;
        if(params.get("number")!=null && !params.get("number").equals("")){
            number = params.get("number").toString();
        }
        Integer statu = -1;
        if(params.get("statu")!=null && !params.get("statu").equals("")){
            statu = Integer.parseInt(params.get("statu").toString());
        }
        String startime = null;
        if(params.get("startime")!=null && !params.get("startime").equals("")){
            startime = params.get("startime").toString();
        }
        String endtime = null;
        if(params.get("endtime")!=null && !params.get("endtime").equals("")){
            endtime = params.get("endtime").toString();
        }

        OnshelvesStatisticsEntity onshelvesStatisticsEntity = baseMapper.getStatistics(shelvesid,number,statu,startime,endtime);

        return onshelvesStatisticsEntity;
    }

}
